package com.nuc.kl.module.usermanager.action;


import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.base.BaseAction;
import com.nuc.kl.module.usermanager.entity.Privilege;
import com.nuc.kl.module.usermanager.entity.Role;
import com.nuc.kl.module.usermanager.entity.TempPrivilege;
import com.nuc.kl.module.usermanager.entity.TreeDTO;
import com.nuc.kl.module.usermanager.entity.User;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role>{
	
	private String privilegeIds;
	private List<TempPrivilege> tempPrivilegeList;
	
	private Long roleIds[];
	//新增
	private String ids;// 删除所用多个id
	private String findName;// 模糊查询所用

	private JSONObject result;// 返回的json
	private String rows;// 每页显示的记录数
	private String page;// 当前第几页
	
	
	/** 列表 */
	public String listJson() throws Exception {
		// 当前页
		int intPage = Integer.parseInt((page == null || page == "0") ? "1"
				: page);
		// 每页显示条数
		int number = Integer.parseInt((rows == null || rows == "0") ? "10"
				: rows);
		// 每页的开始记录 第一页为1 第二页为number +1
		int start = (intPage - 1) * number;
		
		List<Role> list = null;
		
		if(findName == null){
			list = roleService.findAllPage(start, number);
		}else{
			//list = userService.findBySQL(start, number, findName);
		}
		
		String[] s= {"peoples","privileges","roles","roleJson"};
		toBeJson(list, roleService.count(model),s );
		return null;
	}
	
	/** 列表 */
	public String list() throws Exception {
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList",roleList);
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		roleService.delete(model.getId());
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		
		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		roleService.save(model);
		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().put("role", role);
		
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		Role role = roleService.getById(model.getId());
		role.setName(model.getName());
		role.setDescription(model.getDescription());
		roleService.update(role);
		return "toList";
	}
	
	/** 同时删除多个 */
	public String deleteByIds() throws Exception {
		String[] idList = ids.split(",");
		for (String str : idList) {
			roleService.delete(Long.parseLong(str));
		}
		return "toList";
	}
	
	/** 设置权限页面 */
	public String setPrivilegeUI() throws Exception {
		String id = null;
		if(model.getId()!=null)
		{
			id=model.getId().toString();
		}
		List<TreeDTO> treeList = privilegeService.getChildrenByParentId(id);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(JSONArray.fromObject(treeList).toString());
		
			
		return null;
	}
	
	/** 设置权限 */
	public String setPrivilege() throws Exception {
		// 1，从数据库中获取原对象
		Role role = roleService.getById(model.getId());
		Long[] privilegeIds2 = change(privilegeIds);
		// 2，设置要修改的属性
		
		List<Privilege> privilegeList = privilegeService.getByIds(privilegeIds2);
		role.setPrivileges(new HashSet<Privilege>(privilegeList));

		// 3，更新到数据库
		roleService.update(role);

		return null;
	}
	
	private Long[] change(String target){
		String[] ids = target.split(",");
		Long[] result = new Long[ids.length];
		for(int i=0;i<ids.length;i++){
			result[i]=Long.parseLong(ids[i]);
		}
		return result;
	}
	
	
	public List<TempPrivilege> getTempPrivilegeList() {
		return tempPrivilegeList;
	}

	public void setTempPrivilegeList(List<TempPrivilege> tempPrivilegeList) {
		this.tempPrivilegeList = tempPrivilegeList;
	}

	public String getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(String privilegeIds) {
		this.privilegeIds = privilegeIds;
	}

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	
}
